﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	[Authorize]
	public class UserNoteController : DefaultController
	{
		public UserNoteController(IUserNoteService userNoteService)
		{
			this.UserNoteService = userNoteService;
		}

		public IUserNoteService UserNoteService
		{
			get; 
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Index(int id, int? page)
		{
			var notes = UserNoteService.GetPaged(new UserNoteSpecification
			{
				UserId = id
			});

			var note = notes.FirstOrDefault();

			return note.CanView ? View(Views.Index, notes) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Create(int id)
		{
			var note = UserNoteService.Create();

			return note.CanCreate ? View(Views.Create, new UserNoteCreate(id)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(UserNoteCreate value)
		{
			try
			{
				var note = UserNoteService.Create();

				if (!note.CanCreate)
				{
					return NotAuthorized();
				}

				UserNoteService.Insert(note, value);

				return Message(Routes.UserNoteUpdate, new { id = note.Id }, Resources.Messages.UserNoteCreated.Fmt(note.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Update(int id)
		{
			var note = UserNoteService.GetById(id);

			if (note == null)
			{
				return base.Error(Resources.Messages.UserNoteNotFound);
			}

			return note.CanUpdate ? View(Views.Update, new UserNoteUpdate(note)) : NotAuthorized();
		}
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Update(UserNoteUpdate value)
		{
			try
			{
				var note = UserNoteService.GetById(value.Id);

				if (note == null)
				{
					return base.Error(Resources.Messages.UserNoteNotFound);
				}

				if (!note.CanUpdate)
				{
					return NotAuthorized();
				}

				UserNoteService.Update(note, value);

				return Message(Routes.UserNoteUpdate, new { id = note.Id }, Resources.Messages.UserNoteUpdated.Fmt(note.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Delete(int id)
		{
			var note = this.UserNoteService.GetById(id);

			if (note == null)
			{
				return base.Error(Resources.Messages.UserNoteNotFound);
			}

			return note.CanDelete ? base.View(Views.Delete, new UserNoteDelete(note)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(UserNoteDelete value)
		{
			try
			{
				var note = this.UserNoteService.GetById(value.Id);

				if (note == null)
				{
					return base.Error(Resources.Messages.UserNoteNotFound);
				}

				if (!note.CanDelete)
				{
					return NotAuthorized();
				}

				this.UserNoteService.Delete(note);

				return base.RedirectToRoute(Routes.UserNoteIndex);
			}
			catch (ValidationException exception)
			{
				base.ModelState.CopyValidationExceptions(exception);
			}

			return base.View(Views.Delete, value);
		}
	}
}